<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/8
 * Time: 15:08
 */
namespace App\Http\Middleware;

use App\Constant\ErrorConstant;
use App\MultiTrait\ResponseTrait;

class ApiCheckMiddleware
{
    use ResponseTrait;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        /**
         * 检测header头或者post体中带有token
         */
        if (empty($user = auth('api')->user())){
            return $this->errorResponse(ErrorConstant::API_LOGIN_TOKEN_ERROR);
        }

        $api_user['nickname'] =   $user->nickname;
        $api_user['user_id']  =   $user->uid;
        $api_user['unique_id']=   $user->unique_id;
        $api_user['openid']   =   $user->xcx_openid;
        $api_user['balance']  =   $user->balance;
        $api_user['headimg']  =   $user->headimg;
        $request->merge(['api_user' => $api_user]);
        return $next($request);
    }
}

